package com.huawei.hms.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.adapter.a.c;
import com.huawei.hms.adapter.internal.CommonCode.MapKey;
import com.huawei.hms.common.internal.RequestHeader;
import com.huawei.hms.common.internal.ResponseHeader;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hms.support.api.PendingResultImpl;
import com.huawei.hms.support.api.ResolveResult;
import com.huawei.hms.support.api.client.ApiClient;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.Result;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.JsonUtil;
import com.huawei.hms.utils.Util;
import java.lang.ref.WeakReference;
import java.util.Map;

public class BaseAdapter {
    private WeakReference<ApiClient> a;
    private WeakReference<Activity> b;
    private BaseCallBack c;
    private String d;
    private Context e;
    private RequestHeader f = new RequestHeader();
    private ResponseHeader g = new ResponseHeader();
    private c h = new c(this) {
        final /* synthetic */ BaseAdapter a;

        {
            this.a = r1;
        }

        public void a(Intent intent, String str) {
            long j = 0;
            if (TextUtils.isEmpty(str)) {
                HMSLog.e("BaseAdapter", "onSolutionResult but id is null");
                BaseCallBack a = this.a.b();
                if (a == null) {
                    HMSLog.e("BaseAdapter", "onSolutionResult baseCallBack null");
                    a();
                    return;
                }
                a.onError(this.a.a(-6));
                a();
            } else if (str.equals(this.a.d)) {
                HMSLog.i("BaseAdapter", "onSolutionResult + id is :" + str);
                BaseCallBack a2 = this.a.b();
                if (a2 == null) {
                    HMSLog.e("BaseAdapter", "onResult baseCallBack null");
                    a();
                } else if (intent == null) {
                    HMSLog.e("BaseAdapter", "onSolutionResult but data is null");
                    String a3 = this.a.a(-7);
                    this.a.a(this.a.e, this.a.g, 0);
                    a2.onError(a3);
                    a();
                } else {
                    HMSLog.e("BaseAdapter", "onComplete for on activity result");
                    String stringExtra = intent.getStringExtra(MapKey.JSON_HEADER);
                    String stringExtra2 = intent.getStringExtra(MapKey.JSON_BODY);
                    Object infoFromJsonobject = JsonUtil.getInfoFromJsonobject(stringExtra, "status_code");
                    Object infoFromJsonobject2 = JsonUtil.getInfoFromJsonobject(stringExtra, HianalyticsData.ERROR_CODE);
                    if (intent.hasExtra(MapKey.HMS_FOREGROUND_RES_UI)) {
                        Object infoFromJsonobject3 = JsonUtil.getInfoFromJsonobject(intent.getStringExtra(MapKey.HMS_FOREGROUND_RES_UI), "uiDuration");
                        if (infoFromJsonobject3 != null && (infoFromJsonobject3 instanceof Long)) {
                            j = ((Long) infoFromJsonobject3).longValue();
                        }
                    }
                    if (infoFromJsonobject == null || !(infoFromJsonobject instanceof Integer) || infoFromJsonobject2 == null || !(infoFromJsonobject2 instanceof Integer)) {
                        this.a.a(-8);
                        this.a.a(this.a.e, this.a.g, j);
                    } else {
                        int intValue = ((Integer) infoFromJsonobject).intValue();
                        this.a.a(((Integer) infoFromJsonobject2).intValue());
                        this.a.g.setStatusCode(intValue);
                        this.a.a(this.a.e, this.a.g, j);
                    }
                    a2.onComplete(stringExtra, stringExtra2, null);
                    a();
                }
            }
        }

        private void a() {
            if (this.a.h != null) {
                com.huawei.hms.adapter.a.a.b().b(this.a.h);
                this.a.h = null;
            }
        }

        public void a(int i) {
        }
    };

    public interface BaseCallBack {
        void onComplete(String str, String str2, Parcelable parcelable);

        void onError(String str);
    }

    class a implements ResultCallback<ResolveResult<CoreBaseResponse>> {
        final /* synthetic */ BaseAdapter a;

        private a(BaseAdapter baseAdapter) {
            this.a = baseAdapter;
        }

        public /* synthetic */ void onResult(Object obj) {
            a((ResolveResult) obj);
        }

        public void a(ResolveResult<CoreBaseResponse> resolveResult) {
            BaseCallBack a = this.a.b();
            if (a == null) {
                HMSLog.e("BaseAdapter", "onResult baseCallBack null");
            } else if (resolveResult == null) {
                HMSLog.e("BaseAdapter", "result null");
                a.onError(this.a.a(-1));
            } else {
                CoreBaseResponse coreBaseResponse = (CoreBaseResponse) resolveResult.getValue();
                if (coreBaseResponse == null) {
                    HMSLog.e("BaseAdapter", "response null");
                    a.onError(this.a.a(-1));
                } else if (TextUtils.isEmpty(coreBaseResponse.getJsonHeader())) {
                    HMSLog.e("BaseAdapter", "jsonHeader null");
                    a.onError(this.a.a(-1));
                } else {
                    JsonUtil.jsonToEntity(coreBaseResponse.getJsonHeader(), this.a.g);
                    this.a.a(this.a.e, this.a.g);
                    if ("intent".equals(this.a.g.getResolution())) {
                        Activity d = this.a.a();
                        if (d == null || d.isFinishing()) {
                            HMSLog.e("BaseAdapter", "activity null");
                            a.onError(this.a.a(-3));
                            return;
                        }
                        Parcelable pendingIntent = coreBaseResponse.getPendingIntent();
                        if (pendingIntent != null) {
                            this.a.a(d, pendingIntent);
                            return;
                        }
                        Parcelable intent = coreBaseResponse.getIntent();
                        if (intent != null) {
                            this.a.a(d, intent);
                            return;
                        }
                        HMSLog.e("BaseAdapter", "hasResolution is true but NO_SOLUTION");
                        a.onError(this.a.a(-4));
                        return;
                    }
                    HMSLog.i("BaseAdapter", "baseCallBack.onComplete");
                    Parcelable pendingIntent2 = coreBaseResponse.getPendingIntent();
                    if (pendingIntent2 != null) {
                        a.onComplete(coreBaseResponse.getJsonHeader(), coreBaseResponse.getJsonBody(), pendingIntent2);
                        return;
                    }
                    pendingIntent2 = coreBaseResponse.getIntent();
                    if (pendingIntent2 != null) {
                        a.onComplete(coreBaseResponse.getJsonHeader(), coreBaseResponse.getJsonBody(), pendingIntent2);
                    } else {
                        a.onComplete(coreBaseResponse.getJsonHeader(), coreBaseResponse.getJsonBody(), null);
                    }
                }
            }
        }
    }

    static class b extends PendingResultImpl<ResolveResult<CoreBaseResponse>, CoreBaseResponse> {
        public /* synthetic */ Result onComplete(IMessageEntity iMessageEntity) {
            return a((CoreBaseResponse) iMessageEntity);
        }

        public b(ApiClient apiClient, String str, IMessageEntity iMessageEntity) {
            super(apiClient, str, iMessageEntity);
        }

        public ResolveResult<CoreBaseResponse> a(CoreBaseResponse coreBaseResponse) {
            ResolveResult<CoreBaseResponse> resolveResult = new ResolveResult(coreBaseResponse);
            resolveResult.setStatus(Status.SUCCESS);
            return resolveResult;
        }
    }

    public BaseAdapter(ApiClient apiClient) {
        this.a = new WeakReference(apiClient);
    }

    public BaseAdapter(ApiClient apiClient, Activity activity) {
        this.a = new WeakReference(apiClient);
        this.b = new WeakReference(activity);
        this.e = activity.getApplicationContext();
    }

    public void baseRequest(String str, String str2, Parcelable parcelable, BaseCallBack baseCallBack) {
        if (this.a == null) {
            HMSLog.e("BaseAdapter", "client is null");
            baseCallBack.onError(a(-2));
            return;
        }
        ApiClient apiClient = (ApiClient) this.a.get();
        this.c = baseCallBack;
        JsonUtil.jsonToEntity(str, this.f);
        CoreBaseRequest coreBaseRequest = new CoreBaseRequest();
        coreBaseRequest.setJsonObject(str2);
        coreBaseRequest.setJsonHeader(str);
        coreBaseRequest.setParcelable(parcelable);
        String apiName = this.f.getApiName();
        if (TextUtils.isEmpty(apiName)) {
            HMSLog.e("BaseAdapter", "get uri null");
            baseCallBack.onError(a(-5));
            return;
        }
        this.d = this.f.getTransactionId();
        if (TextUtils.isEmpty(this.d)) {
            HMSLog.e("BaseAdapter", "get transactionId null");
            baseCallBack.onError(a(-6));
            return;
        }
        HMSLog.i("BaseAdapter", "in baseRequest + uri is :" + apiName + ", transactionId is : " + this.d);
        a(this.e, this.f);
        a(apiClient, apiName, coreBaseRequest).setResultCallback(new a());
    }

    private PendingResult<ResolveResult<CoreBaseResponse>> a(ApiClient apiClient, String str, CoreBaseRequest coreBaseRequest) {
        return new b(apiClient, str, coreBaseRequest);
    }

    private Activity a() {
        if (this.b == null) {
            return null;
        }
        ApiClient apiClient = (ApiClient) this.a.get();
        if (apiClient == null) {
            return null;
        }
        return Util.getActiveActivity((Activity) this.b.get(), apiClient.getContext());
    }

    private void a(Activity activity, Parcelable parcelable) {
        HMSLog.i("BaseAdapter", "startResolution");
        if (this.f != null) {
            b(this.e, this.f);
        }
        com.huawei.hms.adapter.a.a.b().a(this.h);
        Intent intentStartBridgeActivity = BridgeActivity.getIntentStartBridgeActivity(activity, com.huawei.hms.adapter.b.a.class.getName());
        Bundle bundle = new Bundle();
        bundle.putParcelable(MapKey.HAS_RESOLUTION, parcelable);
        intentStartBridgeActivity.putExtras(bundle);
        intentStartBridgeActivity.putExtra("transaction_id", this.d);
        activity.startActivity(intentStartBridgeActivity);
    }

    private BaseCallBack b() {
        if (this.c != null) {
            return this.c;
        }
        HMSLog.e("BaseAdapter", "callback null");
        return null;
    }

    private void a(Context context, RequestHeader requestHeader) {
        Map mapFromRequestHeader = HiAnalyticsUtil.getInstance().getMapFromRequestHeader(requestHeader);
        mapFromRequestHeader.put("direction", "req");
        mapFromRequestHeader.put("version", String.valueOf(requestHeader.getKitSdkVersion()));
        HiAnalyticsUtil.getInstance().onNewEvent(context, HiAnalyticsConstant.HMS_SDK_BASE_CALL_AIDL, mapFromRequestHeader);
    }

    private void b(Context context, RequestHeader requestHeader) {
        Map mapFromRequestHeader = HiAnalyticsUtil.getInstance().getMapFromRequestHeader(requestHeader);
        mapFromRequestHeader.put("direction", "req");
        mapFromRequestHeader.put("version", String.valueOf(requestHeader.getKitSdkVersion()));
        HiAnalyticsUtil.getInstance().onNewEvent(context, HiAnalyticsConstant.HMS_SDK_BASE_START_RESOLUTION, mapFromRequestHeader);
    }

    private void a(Context context, ResponseHeader responseHeader) {
        HiAnalyticsUtil.getInstance();
        Map mapFromRequestHeader = HiAnalyticsUtil.getMapFromRequestHeader(responseHeader);
        mapFromRequestHeader.put("direction", "rsp");
        mapFromRequestHeader.put("version", String.valueOf(this.f.getKitSdkVersion()));
        HiAnalyticsUtil.getInstance().onNewEvent(context, HiAnalyticsConstant.HMS_SDK_BASE_CALL_AIDL, mapFromRequestHeader);
    }

    private void a(Context context, ResponseHeader responseHeader, long j) {
        HiAnalyticsUtil.getInstance();
        Map mapFromRequestHeader = HiAnalyticsUtil.getMapFromRequestHeader(responseHeader);
        mapFromRequestHeader.put("direction", "rsp");
        mapFromRequestHeader.put("waitTime", String.valueOf(j));
        mapFromRequestHeader.put("version", String.valueOf(this.f.getKitSdkVersion()));
        HiAnalyticsUtil.getInstance().onNewEvent(context, HiAnalyticsConstant.HMS_SDK_BASE_START_RESOLUTION, mapFromRequestHeader);
    }

    private String a(int i) {
        this.g.setTransactionId(this.f.getTransactionId());
        this.g.setAppID(this.f.getAppID());
        this.g.setApiName(this.f.getApiName());
        this.g.setSrvName(this.f.getSrvName());
        this.g.setPkgName(this.f.getPkgName());
        this.g.setStatusCode(1);
        this.g.setErrorCode(i);
        this.g.setErrorReason("Core error");
        return this.g.toJson();
    }
}
